
#include "SQLIndexOptions.h"

#include "SQLIndexDefinition.h"
#include "../visitor/SQLASTVisitor.h"

SQLIndexOptions::SQLIndexOptions()
{
  otherOptions = SQLAssignItem_list_ptr();
}

string_ptr SQLIndexOptions::getIndexType()
{
  return indexType;
}

void SQLIndexOptions::setIndexType(string_ptr indexType)
{
  this->indexType = indexType;
}

SQLExpr_ptr SQLIndexOptions::getKeyBlockSize()
{
  return keyBlockSize;
}

BOOL_ptr SQLIndexOptions::isGlobal()
{
  return global;
}

void SQLIndexOptions::setGlobal(BOOL_ptr global)
{
  this->global = global;
}

BOOL_ptr SQLIndexOptions::isLocal()
{
  return local;
}

void SQLIndexOptions::setLocal(BOOL_ptr local)
{
  this->local = local;
}

void SQLIndexOptions::setKeyBlockSize(SQLExpr_ptr keyBlockSize)
{
  if (keyBlockSize != NULL)
  {
    if (getParent() != NULL && getParent()->getParent() != NULL)
    {
      keyBlockSize->setParent(getParent()->getParent());
    }
    else
    {
      keyBlockSize->setParent(SharedObject(SQLIndexOptions));
    }
  }
  this->keyBlockSize = keyBlockSize;

  // Old parser code put it in options list->
  // if (keyBlockSize != NULL && getParent() != NULL && getParent() instanceof SQLIndexDefinition *)
  // {
  //     SQLIndexDefinition *parent = (SQLIndexDefinition *)getParent();
  //     SQLAssignItem *assignItem = new SQLAssignItem *(new SQLIdentifierExpr("KEY_BLOCK_SIZE"), keyBlockSize);
  //     if (getParent() != NULL && getParent()->getParent() != NULL)
  //     {
  //         assignItem->setParent(getParent()->getParent());
  //     }
  //     else
  //     {
  //         assignItem->setParent(this);
  //     }
  //     parent->getCompatibleOptions()->add(assignItem);
  // }
}

string_ptr SQLIndexOptions::getParserName()
{
  return parserName;
}

void SQLIndexOptions::setParserName(string_ptr parserName)
{
  this->parserName = parserName;
}

SQLExpr_ptr SQLIndexOptions::getComment()
{
  return comment;
}

void SQLIndexOptions::setComment(SQLExpr_ptr comment)
{
  if (comment != NULL)
  {
    if (getParent() != NULL && getParent()->getParent() != NULL)
    {
      comment->setParent(getParent()->getParent());
    }
    else
    {
      comment->setParent(SharedObject(SQLIndexOptions));
    }
  }
  this->comment = comment;
}

string_ptr SQLIndexOptions::getAlgorithm()
{
  return algorithm;
}

void SQLIndexOptions::setAlgorithm(string_ptr algorithm)
{
  this->algorithm = algorithm;

  // Old parser code put it in options list->
  // if (algorithm != NULL && getParent() != NULL && getParent() instanceof SQLIndexDefinition *)
  // {
  //     SQLIndexDefinition *parent = (SQLIndexDefinition *)getParent();
  //     SQLAssignItem *assignItem = new SQLAssignItem *(new SQLIdentifierExpr("ALGORITHM"), new SQLIdentifierExpr(algorithm));
  //     if (getParent() != NULL && getParent()->getParent() != NULL)
  //     {
  //         assignItem->setParent(getParent()->getParent());
  //     }
  //     else
  //     {
  //         assignItem->setParent(this);
  //     }
  //     parent->getCompatibleOptions()->add(assignItem);
  // }
}

string_ptr SQLIndexOptions::getLock()
{
  return lock;
}

void SQLIndexOptions::setLock(string_ptr lock)
{
  this->lock = lock;

  // Old parser code put it in options list->
  // if (lock != NULL && getParent() != NULL && getParent() instanceof SQLIndexDefinition *)
  // {
  //     SQLIndexDefinition *parent = (SQLIndexDefinition *)getParent();
  //     SQLAssignItem *assignItem = new SQLAssignItem *(new SQLIdentifierExpr("LOCK"), new SQLIdentifierExpr(lock));
  //     if (getParent() != NULL && getParent()->getParent() != NULL)
  //     {
  //         assignItem->setParent(getParent()->getParent());
  //     }
  //     else
  //     {
  //         assignItem->setParent(this);
  //     }
  //     parent->getCompatibleOptions()->add(assignItem);
  // }
}

BOOL_ptr SQLIndexOptions::isInvisible()
{
  return invisible;
}

void SQLIndexOptions::setInvisible(BOOL_ptr invisible)
{
  this->invisible = invisible;
}

BOOL_ptr SQLIndexOptions::isVisible()
{
  return visible;
}

void SQLIndexOptions::setVisible(BOOL_ptr visible)
{
  this->visible = visible;
}

SQLAssignItem_list_ptr SQLIndexOptions::getOtherOptions()
{
  return otherOptions;
}

void SQLIndexOptions::accept0(SQLASTVisitor_ptr visitor)
{
  visitor->visit(SharedObject(SQLIndexOptions));
  visitor->endVisit(SharedObject(SQLIndexOptions));
}

void SQLIndexOptions::cloneTo(SQLIndexOptions_ptr options)
{
  SQLObject_ptr parent;
  if (options->getParent() != NULL && options->getParent()->getParent() != NULL)
  {
    parent = options->getParent()->getParent();
  }
  else
  {
    parent = std::shared_ptr<SQLIndexOptions>(options);
  }
  options->indexType = indexType;
  if (keyBlockSize != NULL)
  {
    options->keyBlockSize = std::dynamic_pointer_cast<SQLExpr>(keyBlockSize->clone());
    options->keyBlockSize->setParent(parent);
  }
  options->parserName = parserName;
  if (comment != NULL)
  {
    options->comment = std::dynamic_pointer_cast<SQLExpr>(comment->clone());
    options->comment->setParent(parent);
  }
  options->algorithm = algorithm;
  options->lock = lock;
  for (SQLAssignItem_ptr item : *otherOptions)
  {
    SQLAssignItem_ptr item1 = std::dynamic_pointer_cast<SQLAssignItem>(item->clone());
    item1->setParent(parent);
    options->otherOptions->push_back(item1);
  }

  options->invisible = invisible;

  options->global = global;
  options->local = local;
}
